package com.hainatong.shiphelper.utils;



import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.ImageView;
import androidx.annotation.IdRes;

import androidx.databinding.BindingAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.hainatong.shiphelper.R;

//import jp.wasabeef.glide.transformations.BlurTransformation;
/**
 * 加载图片静态工具
 */
public class GlideUtil
{

    private static GlideUtil instance;

    private GlideUtil()
    {
    }

    public static GlideUtil getInstance()
    {
        if (instance == null)
        {
            instance = new GlideUtil();
        }
        return instance;
    }


    /**
     * 显示随机的图片(每日推荐)
     *
     * @param imgNumber 有几张图片要显示,对应默认图
     * @param imageUrl  显示图片的url
     * @param imageView 对应图片控件
     */
    public static void displayRandom(int imgNumber, String imageUrl, ImageView imageView)
    {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .transition(DrawableTransitionOptions.withCrossFade(1500))
                .into(imageView);
    }



//--------------------------------------

    /**
     * 用于干货item，将gif图转换为静态图
     */
    public static void displayGif(String url, ImageView imageView)
    {
        //DensityUtil.dip2px
        Glide.with(imageView.getContext())
//                .asBitmap()
                .load(url)
                .override( 60, 80)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
//                .skipMemoryCache(true) //跳过内存缓存
//                .crossFade(1000)
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// 缓存图片源文件（解决加载gif内存溢出问题）
//                .into(new GlideDrawableImageViewTarget(imageView, 1));
                .into(imageView);
    }


    /**
     * 显示高斯模糊效果（电影详情页）
     */
    private static void displayGaussian(Context context, String url, ImageView imageView)
    {
        // "23":模糊度；"4":图片缩放4倍后再进行模糊
        Glide.with(context)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                //高斯模糊
                //.transform(new BlurTransformation(50, 8))
                .into(imageView);
    }

    /**
     * 加载圆角图,暂时用到显示头像
     */
    @BindingAdapter("android:displayCircle")
    public static void displayCircle(ImageView imageView, String imageUrl)
    {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .error(R.drawable.shape_img_placeholder)
                .transform(new CircleCrop())
//                .apply(bitmapTransform(new CircleCrop()))
//                .transform(new GlideCircleTransform())
//                .transform(new RoundedCorners(20))
//                .transform(new CenterCrop(), new RoundedCorners(20))
                .into(imageView);
    }


    /**
     * 电影详情页显示电影图片(等待被替换)（测试的还在，已可以弃用）
     * 没有加载中的图
     */
    @BindingAdapter("android:showImg")
    public static void showImg(ImageView imageView, String url)
    {
        Glide.with(imageView.getContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .error(R.drawable.shape_img_placeholder)
                .into(imageView);
    }


    @SuppressLint("ResourceType")
    @BindingAdapter("android:localImg")
    public static void localImg(ImageView imageView, @IdRes int iconRes)
    {
        Glide.with(imageView.getContext())
                .load(iconRes)
                .into(imageView);
    }




    /**
     * 电影详情页显示高斯背景图
     */
    @BindingAdapter("android:showImgBg")
    public static void showImgBg(ImageView imageView, String url) {
        displayGaussian(imageView.getContext(), url, imageView);
    }


    /**
     * 热门电影头部图片
     */
    @BindingAdapter({"android:displayRandom", "android:imgType"})
    public static void displayRandom(ImageView imageView, int imageUrl, int imgType)
    {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .transition(DrawableTransitionOptions.withCrossFade(1500))
                .into(imageView);
    }

    /**
     * 加载固定宽高图片
     */
    @BindingAdapter({"android:imageUrl", "android:imageWidthDp", "android:imageHeightDp"})
    public static void imageUrl(ImageView imageView, String url, int imageWidthDp, int imageHeightDp)
    {
        //DensityUtil.dip2px(imageView.getContext(), imageWidthDp), DensityUtil.dip2px(imageView.getContext(), imageHeightDp)
        Glide.with(imageView.getContext())
                .load(url)
                .override(imageWidthDp,imageHeightDp)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .placeholder(R.mipmap.ic_launcher)
                .centerCrop()
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }
}
